package cn.ekuma.pos.pda.inventory;

import java.io.Serializable;
import java.util.List;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import com.openbravo.bean.Product;
import com.openbravo.bean.SharedProduct;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBFCompareEnum;
import cn.ekuma.epos.android.finder.ProductFinderActivate;
import cn.ekuma.epos.datalogic.DataLogicPDA;
import cn.ekuma.epos.datalogic.I_DataLogicERP;

public class ProductViewActivate extends ProductFinderActivate{

	  public void onCreateContextMenu(ContextMenu menu,
	                                  View v,
	                                  ContextMenu.ContextMenuInfo menuInfo) {
	    int position = ((AdapterView.AdapterContextMenuInfo) menuInfo).position;
	    menu.add(Menu.NONE, 0, position, "条码相似商品");
	    menu.add(Menu.NONE, 1, position, "批号库存");
	    menu.add(Menu.NONE, 2, position, "有发生货客商");
	    menu.add(Menu.NONE, 3, position, "运营商品分类");
	    menu.add(Menu.NONE, 4, position, "加入共享库");
	    menu.add(Menu.NONE, 5, position, "商品图片");
	  }

	  public boolean onContextItemSelected(MenuItem item) {
	    int position = item.getItemId();
	    int order=item.getOrder();
	    Product selectObj=(Product) listAdapter.getItem(order);
	    switch(position){
	    case 0:
	    	shwoCodeSimpleProduct(selectObj);
	    	break;
	    case 4:
	    	addProductToShard(selectObj);
	    	break;
	    case 5:
	    	Intent intent3 = new Intent();
	    	intent3.setClass(ProductViewActivate.this, ProductImageActivate.class);
	    	Bundle mBundle3=new Bundle();
	    	mBundle3.putSerializable(ProductImageActivate.SELECTED_BEAN, selectObj);
	    	intent3.putExtras(mBundle3);
	    	startActivity(intent3);
	    	break;
	    }
	    return true;
	  }
	
	private void addProductToShard(final Product selectObj) {
		final ProgressDialog dialog = ProgressDialog.show(this, "", 
		        "执行中 …", true, true);
		new Thread(){
			@Override
			public void run(){
				I_DataLogicERP dlERP=DataLogicPDA.getDLERP();
				try {
					dlERP.saveSharedProduct(new SharedProduct(selectObj.getID()));
				} catch (BasicException e) {
					mHandler.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败   
				}
				dialog.dismiss(); 
			}
			}.start();
	}

	private void shwoCodeSimpleProduct(Product obj) {
		final Object[] filter=new Object[] {
                QBFCompareEnum.COMP_NONE, null,
                QBFCompareEnum.COMP_NONE, null,
                QBFCompareEnum.COMP_NONE, null,
                QBFCompareEnum.COMP_NONE, null,
                QBFCompareEnum.COMP_RE, obj.getCode().substring(0, 8),
                QBFCompareEnum.COMP_DISTINCT, obj.getID()
            };
		final ProgressDialog dialog = ProgressDialog.show(this, "", 
		        "查旬中，请稍等 …", true, true);
		new Thread(){
			@Override
			public void run(){
				I_DataLogicERP dlERP=DataLogicPDA.getDLERP();
				try {
					List<Product> lists=dlERP.listByFilterWithClassName(Product.class.getName(), filter);
					if(lists!=null){
					   Intent intent = new Intent();
			    	   intent.setClass(ProductViewActivate.this, ProductFinderActivate.class);
			    	   Bundle mBundle=new Bundle();
			    	   mBundle.putSerializable(ProductFinderActivate.LIST_PRODUCTS, (Serializable) lists);
			    	   intent.putExtras(mBundle);
			    	   startActivity(intent);
					}
				} catch (BasicException e) {
					mHandler.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败   
				}
				dialog.dismiss(); 
			}
			}.start();
	}

	public void baseBeanSelected(Product obj) {
		Intent intent = new Intent();
    	intent.setClass(ProductViewActivate.this, ProductDetailViewActivate.class);
    	Bundle mBundle=new Bundle();
    	mBundle.putSerializable(ProductDetailViewActivate.SELECTED_BEAN, obj);
    	intent.putExtras(mBundle);
    	startActivity(intent);
	}
 }